<?php

namespace app\erp\model;

use think\Model;
use app\jun\controller\Api;

class Offerdb extends Model
{
    protected $pk = 'offer_id';
    protected $name = 'offer';
    protected $type = [
        'create_time' => 'timestamp:Y-m-d H:i',
        'audit_time' => 'timestamp:Y-m-d H:i'
    ];
    protected $autoWriteTimestamp = true;
    public $param = array();

    public function base($query)
    {
        if (isset ($this->param ['offer_id']) && $this->param ['offer_id']) {
            $query->offerId($this->param ['offer_id']);
        }
        if (isset ($this->param ['supplier_id']) && $this->param ['supplier_id']) {
            $query->supplierId($this->param ['supplier_id']);
        }
        if (isset ($this->param ['audit_state']) && $this->param ['audit_state']) {
            $query->auditState($this->param ['audit_state']);
        }
        if (isset ($this->param ['supplier_name']) && $this->param ['supplier_name']) {
            $query->supplierName($this->param ['supplier_name']);
        }
        if (isset ($this->param ['baobei_order_no']) && $this->param ['baobei_order_no']) {
            $query->baobeiorderNo($this->param ['baobei_order_no']);
        }
        if (isset ($this->param ['create_time_start']) && $this->param ['create_time_start']) {
            $query->createTimeStart($this->param ['create_time_start']);
        }
        if (isset ($this->param ['create_time_end']) && $this->param ['create_time_end']) {
            $query->createTimeEnd($this->param ['create_time_end']);
        }
        if (isset ($this->param ['is_used']) && $this->param ['is_used']) {
            $query->isUsed($this->param ['is_used']);
        }
    }

    public function scopeIsUsed($query, $is_used)
    {
        $query->where('is_used', $is_used);
    }

    public function scopeCreateTimeStart($query, $create_time)
    {
        $query->whereTime('create_time', '>=', $create_time);
    }

    public function scopeCreateTimeEnd($query, $create_time)
    {
        $time = strtotime($create_time);
        $date = strtotime('+1 day', $time);
        $query->whereTime('create_time', '<', $date);
    }

    public function scopeSupplierName($query, $susername)
    {
        $memberdb = new Memberdb ();
        $get_supplier_id = $memberdb->useGlobalScope(false)->where('supplier_name', 'like', '%' . $susername . '%')->where('is_supplier', 1)->select();
        if (!$get_supplier_id->isEmpty()) {
            $member_ids = array();
            $get_supplier_id = $get_supplier_id->toArray();
            foreach ($get_supplier_id as $key => $val) {
                array_push($member_ids, $val ['member_id']);
            }
            $query->where('supplier_id', 'IN', $member_ids);
        } else {
            $query->where('supplier_id', -1);
        }
    }

    public function scopeOfferId($query, $offer_id)
    {
        $query->where('offer_id', $offer_id);
    }

    public function scopeSupplierId($query, $supplier_id)
    {
        $query->where('supplier_id', $supplier_id);
    }

    public function scopeAuditState($query, $audit_state)
    {
        $query->where('audit_state', $audit_state);
    }

    public function scopeBaobeiOrderNo($query, $baobei_order_no)
    {
        $query->where('baobei_order_no', $baobei_order_no);
    }

    public function ajaxSave()
    {

        if (isset ($this->param ['offer_id']) && $this->param ['offer_id']) {
            $this->param['audit_admin_id'] = MEMBER_ID;
            $this->param['audit_time'] = time();
            if (isset ($this->param ['audit_state']) && $this->param ['audit_state'] == 1) {
                // 审核通过，推送接口
                $get_data = $this->useGlobalScope(false)->where('offer_id', $this->param ['offer_id'])->find()->toArray();
                $param_arr = json_decode($get_data ['param'], true);
                $post_json ['priceList'] = array();
                $sku_arr = array();
                foreach ($param_arr as $key => $val) {
                    $temp = array();
                    $temp ['sku'] = $val ['goods_sku'];
                    $temp ['price'] = $val ['api_price'];
                    $temp ['stock'] = $val['view_stock'];
                    array_push($post_json ['priceList'], $temp);
                    // 锁定商品价格
                    array_push($sku_arr, $val ['goods_sku']);
                }
                $jun_api = new Api ();
                $jun_api->ajaxCountPrice($post_json);
                $goodsdb = new Goodsdb ();
                $goods_save_data ['is_lock'] = 1;
                $goods_save_data ['lock_open_time'] = time() + 5 * 60 * 60; // 锁定时间 5小时
                $goodsdb->where('goods_sku', 'IN', $sku_arr)->update($goods_save_data);
            }

            $this->allowField(true)->save($this->param, [
                'offer_id' => $this->param ['offer_id']
            ]);
        } else {
            $this->allowField(true)->save($this->param);
        }
        return 'success';
    }

    public function ajaxFind()
    {
        $data = $this->find();
        if ($data) {
            $data = $data->toArray();
            $data = $this->handleData($data);
        } else {
            $data = array();
        }
        return $data;
    }

    public function ajaxSelect()
    {
        $return ['data'] = $this->ajaxList();
        $return ['count'] = $this->ajaxCount();
        return $return;
    }

    public function ajaxList()
    {
        $data = $this->order('offer_id desc')->page($this->param ['page'], $this->param ['limit'])->select();
        if (!$data->isEmpty()) {
            $data = $data->toArray();
            foreach ($data as $key => $val) {
                $data [$key] = $this->handleData($val);
            }
        } else {
            $data = array();
        }
        return $data;
    }

    public function ajaxCount()
    {
        $count = $this->count();
        return $count;
    }

    public function ajaxDelete()
    {
        if (isset ($this->param ['offer_id'])) {
            $this->destroy(function ($query) {
                $query->where('offer_id', 'IN', $this->param ['offer_id']);
            }, true);
            return 'success';
        }
    }

    // 数据处理
    protected function handleData($val)
    {
        foreach ($val as $k => $v) {
            $val [$k] = $v;
            switch ($k) {
                case 'audit_admin_id':
                    if ($v) {
                        $memberdb = new Memberdb ();
                        $memberdb->param ['member_id'] = $v;
                        $get_admin = $memberdb->useGlobalScope(false)->where('member_id', $v)->find();
                        $val ['audit_admin_name'] = '';
                        if ($get_admin) {
                            $get_admin = $get_admin->toArray();
                            $val ['audit_admin_name'] = $get_admin ['username'];
                        }
                    }
                    break;
                case 'supplier_id' :
                    if ($v) {
                        $memberdb = new Memberdb ();
                        $memberdb->param ['member_id'] = $v;
                        $memberdb->param ['is_supplier'] = 1;
                        $get_supplier = $memberdb->useGlobalScope(false)->where('member_id', $v)->find();
                        $val ['supplier_name'] = '';
                        if ($get_supplier) {
                            $get_supplier = $get_supplier->toArray();
                            $val ['supplier_name'] = $get_supplier ['supplier_name'];
                        }
                    }
                    break;
                case 'param' :
                    $val ['param_arr'] = json_decode($v, true);
                    break;
                case 'pingzheng':
                    if ($v) {
                        $uploaddb = new Uploaddb();
                        $pingzheng_info = $uploaddb->getFind($v);
                        $val['pingzheng_url'] = $pingzheng_info['save_path'];
                        $val['pingzheng_info'] = $pingzheng_info;
                    }
                    break;
                default :
                    break;
            }
        }
        return $val;
    }
}